/**
 * 
 */
package com.xbao.core.util;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.binary.Base64;

/**
 * @author guozw@github.com/xingganfengxing/
 *
 */
public class Base64Util {
	public static String encode(byte[] src) {
		Base64 base64 = new Base64();
		String out = null;
		out = base64.encodeToString(src);
		return out;
	}
	
	public static byte[] encodeToBytes(byte[] str) {
		Base64 base64 = new Base64();
		byte[] out = null;
		try {
			out = base64.encode(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return out;
	}
	
	public static byte[] decodeToBytes(byte[] str) {
		Base64 base64 = new Base64();
		byte[] out = null;
		try {
			out = base64.decode(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return out;
	}

	public static String encode(String str) {
		Base64 base64 = new Base64();
		String out = null;
		try {
			out = base64.encodeToString(str.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return out;
	}

	public static byte[] decodeBase64(String str) {
		return Base64.decodeBase64(str);
	}

	public static String decode(String str) {
		return new String(Base64.decodeBase64(str));
	}
}
